package com.zang.im.croom.netty.handler.websocket;

import com.zang.im.croom.model.protobuf.WSMessageOuterClass;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * 1000 文本消息
 * @author Zhang Qiang
 * @Date 2019/7/29 17:14
 */
@Slf4j
@Service
public class TextHandler {

    public void handleMsg(ChannelHandlerContext ctx, Map<String, Channel> channelMap, WSMessageOuterClass.WSMessage wsMessage){

            String requestText = wsMessage.getMsg();
            log.info("收到消息：" + requestText);

            if (requestText.contains("time")){
                TextWebSocketFrame text = new TextWebSocketFrame(
                        new Date().toString() +" , id :" + ctx.channel().id() + " , 当前在线人数:" + channelMap.size() + ", 回执消息: " + requestText
                );
                log.info("发送消息: " + text.text());
                ctx.channel().writeAndFlush(text);
            }

    }

}
